package com.ccc.dingtalk.service.db.impl;

import com.ccc.dingtalk.mapper.BaseMapper;
import com.ccc.dingtalk.pojo.db.BaseDO;
import com.ccc.dingtalk.service.db.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 通用业务实现
 *
 * @author superC
 * @version 1.0.0
 * @date 2022/3/24
 */

@SuppressWarnings("all")
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO> implements IBaseService<T> {

    @Autowired
    protected M mapper;

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    public T getOne() {
        List<T> list = mapper.findAll();
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean insert(T entity) {
        mapper.insert(entity);
        return true;
    }

    @Override
    public boolean update(T entity) {
        if (entity.getId() == null) {
            throw new IllegalArgumentException("entity ID must not be null");
        }
        mapper.update(entity);
        return true;
    }

    @Override
    public boolean delete(Long id) {
        mapper.delete(id);
        return true;
    }
}
